package com.droidblackbox.log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.droidblackbox.R;
import com.droidblackbox.manager.FileManager;
import com.droidblackbox.manager.SystemManager;

public class Log extends Activity implements OnClickListener {

	private String logMessage;
	private String logDelete;

	private StringBuffer readLog;

	private float textSize = 0.0f;

	private TextView logTextView = null;
	private ImageView deleteView = null;
	
	private Button textUpButton = null;
	private Button textDownButton = null;


	private File log = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.log);

		new AddAsyncTask().execute();

	}

	private static final int DIALOG_PROGRESS = 1;
	private static final int MAX_PROGRESS = 0x70;
	private ProgressDialog progressDialog = null;
	@Override
	public Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_PROGRESS:

			progressDialog = new ProgressDialog(Log.this);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressDialog.setTitle("DrodiBlackBox");
			progressDialog.setMessage("Loading..... Please wait..");
			progressDialog.setCancelable(false);
			progressDialog.setMax(MAX_PROGRESS); // Max값 100
			return progressDialog;

		}
		return null;
	}

	class AddAsyncTask extends AsyncTask<Void, Integer, Void> {

		@Override
		protected void onPreExecute() {
			initCompoment();
			
			showDialog(DIALOG_PROGRESS);

			progressDialog.setProgress(0);
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... unused) {
			openLogFile();
			readLogFile();
			while (logTextView.equals(logMessage)) {
			}
			publishProgress(MAX_PROGRESS);
			return (null);
		}

		@Override
		protected void onProgressUpdate(Integer... progress) {
			progressDialog.setProgress(progress[0]);
		}

		@Override
		protected void onPostExecute(Void unused) {
			dismissDialog(DIALOG_PROGRESS);
			progressDialog.dismiss();

		}

		protected void onCancelled() {
			super.onCancelled();
		}
	}

	private void initCompoment() {
		SystemManager.getInstance().initTitleBar(this);
		logMessage = getResources().getString(R.string.log_message);
		logDelete = getResources().getString(R.string.log_delete);



		deleteView = (ImageView) findViewById(R.id.deleteView);
		logTextView = (TextView) findViewById(R.id.logTextView);

		textSize = getResources().getDimension(R.dimen.log_text_size);
		logTextView.setTextSize(textSize);

		textUpButton = (Button) findViewById(R.id.textUpButton);
		textDownButton = (Button) findViewById(R.id.textDownButton);
		
		textUpButton.setOnClickListener(this);
		textDownButton.setOnClickListener(this);

		deleteView.setOnClickListener(this);
	}

	private void openLogFile() {
		try {
			log = FileManager.getFile(FileManager.FILE_LOG);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void readLogFile() {
		FileInputStream fis;
		try {
			String s;
			fis = new FileInputStream(log);
			InputStreamReader isr = new InputStreamReader(fis);
			BufferedReader br = new BufferedReader(isr);
			while ((s = br.readLine()) != null)
				if (readLog == null) {
					readLog = new StringBuffer();
					readLog.append(s);
				} else {
					readLog.append("\r\n");
					readLog.append(s);
				}
			br.close();
			isr.close();
			fis.close();
		} catch (IOException e) {
		}

		runOnUiThread(new Runnable() {

			public void run() {
				if (readLog == null)
					logTextView.setText(logMessage);
				else
					logTextView.setText(readLog);
			}
		});

	}

	public void onClick(View v) {

		if (v == deleteView) {
			Toast.makeText(getApplicationContext(), logDelete, 0).show();
			log.delete();
			logTextView.setText(logMessage);

			return;
		}

		if (v == textUpButton) {
			textSize++;
		} else if (v == textDownButton) {
			textSize--;
		} else {

		}

		logTextView.setTextSize(textSize);

	}

}
